package com.yingli.component.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.yingli.component.R;

public class NotificationService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private NotificationManager notificationManager;
    private String channelId = "notification1";
    @Override
    public void onCreate() {
        super.onCreate();
        //获取通知管理类
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            Notification notification = new NotificationCompat.Builder(this,channelId)
                    .setContentText("这里是通知文本")
                    .setContentTitle("这里是标题")
                    .setSmallIcon(R.drawable.ic_app)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_app))
                    .build();
            //创建通知渠道
            NotificationChannel channel = new NotificationChannel(channelId,"测试通知",NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(100,notification);
            startForeground(100,notification);
        }else {
            Notification notification =
                    new NotificationCompat.Builder(this)
                            .setContentText("这里是通知文本")
                            .setContentTitle("这里是标题")
                            .setSmallIcon(R.drawable.ic_app)
                            .build();
            notificationManager.notify(100,notification);
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
